package cn.itrip.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.vo.hotel.ItripHotelVO;
import cn.itrip.beans.vo.hotel.SearchHotCityVO;
import cn.itrip.beans.vo.hotel.SearchHotelVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import cn.itrip.service.HotelSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.w3c.dom.ls.LSInput;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2020/3/16 23:17
 * @Version: 1.0
 */
@Controller
@RequestMapping("/api/hotellist")
public class HotelListController {

    private Logger logger = LoggerFactory.getLogger(HotelListController.class);

    @Resource
    private HotelSearchService hotelSearchService;

    /**
     * 查询酒店分页
     * @param searchHotelVO
     * @return
     */
    @RequestMapping(value="/searchItripHotelPage",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto searchItripHotelPage(@RequestBody SearchHotelVO searchHotelVO){

        if(EmptyUtils.isEmpty(searchHotelVO) || EmptyUtils.isEmpty(searchHotelVO.getDestination())){
            return DtoUtil.returnFail("目的地不能为空","20002");
        }
        Page<ItripHotelVO> hotelVOPage = null;
        try {
            hotelVOPage = hotelSearchService.getHotelForPage(searchHotelVO);
            return DtoUtil.returnDataSuccess(hotelVOPage);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查询酒店异常",e);
            return DtoUtil.returnFail("系统异常，获取失败","20001");
        }

    }

    /**
     * 根据热门城市查询酒店
     * @param searchHotCityVO
     * @return
     */
    @RequestMapping(value="/searchItripHotelListByHotCity",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto<List<ItripHotelVO>> searchItripHotelListByHotCity(
            @RequestBody SearchHotCityVO searchHotCityVO){

        if(EmptyUtils.isEmpty(searchHotCityVO) || EmptyUtils.isEmpty(searchHotCityVO.getCityId())){
            return DtoUtil.returnFail("城市Id不能为空","20004");
        }
        try {
            List<ItripHotelVO> list = hotelSearchService.getTopHotelByCityId(searchHotCityVO);
            return DtoUtil.returnDataSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查询热门酒店异常",e);
            return DtoUtil.returnFail("系统异常，获取失败","20003");
        }

    }
}
